package com.qinhailin.portal.dbm.ctrl;

import java.sql.Timestamp;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.qinhailin.common.base.BaseController;
import com.qinhailin.common.routes.ControllerBind;
import com.qinhailin.common.util.AESUtil;
import com.qinhailin.common.util.DateUtil;
import com.qinhailin.portal.dbm.model.CTCustomer;
import com.qinhailin.portal.dbm.po.Data;
import com.qinhailin.portal.dbm.service.CTCustomerServiceImpl;
import com.qinhailin.portal.dbm.service.UploadServiceImpl;

@ControllerBind(path="/portal/dbm/upload")
@Clear
public class UploadController extends BaseController{

	private static final Logger log = Logger.getLogger(UploadController.class);
	
	@Inject
	private CTCustomerServiceImpl customerService;
	@Inject
	private UploadServiceImpl uploadService;
	
	public void index() {
		Integer rCustID = this.getInt("rCustID", 0);
		CTCustomer c = customerService.findByID(rCustID);
		String str = this.get("data");
		Timestamp now = DateUtil.getCurrentTimestamp();
		
		log.info("REC rCustID:" + c.getSID() + ",data:" + (str != null ? (str.length() > 100 ? str.subSequence(0,  100) : str) : "null"));
		//System.out.println("REC rCustID:" + rCustID + ",data:" + (str != null ? (str.length() > 100 ? str.subSequence(0,  100) : str) : "null"));
		String key = null;
		if(c.getXKey() != null && c.getXKey().length() > 0) {
			key = c.getXKey();
		}
		Data data = JSONObject.parseObject(key != null ? AESUtil.decrypt(str, key) : str, Data.class);
		Integer rs = uploadService.save(data);
		this.renderJson(rs);
		log.info("COMPLETE rCustID:" + c.getSID() + ",rQueryID:" + data.getRQueryID() + ",ROWS:" + rs + ",COST:" + (now.getTime() - DateUtil.getCurrentTimestamp().getTime()));
	}
	
}
